package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientUser;

public class UserLoginView extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2426638815209620760L;

	private JTextField loginField;
	
	private ClientUser clientUser;
	

	public UserLoginView(ClientUser clientUser){
		super();
		this.clientUser = clientUser;
		initialize();
	}
	
	public ClientUser getClientUser() {
		return clientUser;
	}

	public void setClientUser(ClientUser clientUser) {
		this.clientUser = clientUser;
	}

	public JTextField getLoginField() {
		return loginField;
	}

	public void setLoginField(JTextField loginField) {
		this.loginField = loginField;
	}

	
	public void initialize(){
		setTitle("User Login"); 
		setSize(300,100);
		setLocationRelativeTo(null); 
		setResizable(false); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		
		
		// Login
		JLabel labelLogin = new JLabel("Login");
		JTextField textField = new JTextField(20);
		this.setLoginField(textField);
		textField.addActionListener(this);
		
		JPanel panelLogin = new JPanel(new BorderLayout());
		panelLogin.add(labelLogin, BorderLayout.WEST);
		panelLogin.add(getLoginField(), BorderLayout.EAST);
		panelLogin.setPreferredSize(new Dimension(300,20));		
		// Form
		JPanel panelForm = new JPanel();
		panelForm.add(panelLogin);
		
		
		// Panel buttons
		JButton btnSend = new JButton("Envoyer");
		btnSend.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						connexion();
					}
				}
		);
		
		JButton btnQuit = new JButton("Quitter");
		btnQuit.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						System.exit(0);
					}
				}
			);

		JPanel panelButton = new JPanel(new BorderLayout());
		panelButton.add(btnSend, BorderLayout.WEST);
		panelButton.add(btnQuit, BorderLayout.CENTER);
		
		
		Container content = getContentPane();
		content.add(panelForm,BorderLayout.CENTER);
		content.add(panelButton,BorderLayout.SOUTH);

		
	}
	
    public void connexion() {
    	if(!loginField.getText().equals("")){
    		this.clientUser.setLogin(loginField.getText());
    		this.clientUser.joinSession();
    	}
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		connexion();
	}

	
}
